#! /usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'huangjacky'
__doc__ = '框架中使用到得一些基类'

from init import lookup
from tornado.web import RequestHandler, HTTPError
from tornado.escape import json_encode

class CGI(RequestHandler):
    """所有CGI的一个基类"""
    uri = "/"  #
    setting = {}  # 在CGI初始化时候需要用到的额外设置

    def initialize(self):
        super(CGI, self).initialize()
        self.lookup = lookup

    def view(self, template_name, data):
        """使用mako来加载视图"""
        template = self.lookup.get_template(template_name)
        self.finish(template.render(**data))

    def return_json(self, data):
        """向客户端中输出json"""
        self.set_header('Content-Type', 'application/json; charset=utf-8')
        self.finish(json_encode(data))
